home *** CD-ROM | disk | FTP | other *** search
/ Windows Expert / Windows Expert.iso / windownt / queue.zip / QUEUE / QSRV.C < prev    next >
C/C++ Source or Header  |  1992-10-10  |  4KB  |  158 lines

  1. /************************************************************************\
  2. * The enclosed files, "the software," is provided by 
  3. * Microsoft Corporation "as is" without warranty of any kind. 
  4. * MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, 
  5. * INCLUDING BUT NOT LIMITED TO IMPLIED WARRANTIES OF MERCHANTABILITY 
  6. * AND FITNESS FOR A PARTICULAR PURPOSE.  You assume all risks of 
  7. * using the software.
  8. * The software is Copyright (c) 1992 Microsoft Corporation.
  9. * Original Author: John M. Hall, Microsoft SDE  9/1/92
  10. *
  11. * You are granted the right to freely distribute this software.
  12. * You are granted the right to make changes provided this comment block
  13. * is retained without modification and you acknowledge the changes.
  14. \************************************************************************/
  15. #define DEBUG
  16. #include <stdio.h>
  17. #include <assert.h>
  18. #include <windows.h>
  19. #include <string.h>
  20. #include <queue.h>
  21. #include <shrmem.h>
  22. #include <myopt.h>
  23.  
  24. #define QUEUE_NAME "\\queues\\test"
  25.  
  26. main(int argc, char *argv[])
  27. {
  28.     HQUEUE  hq;
  29.     DWORD   dwErr;
  30.     HANDLE  hEvent;
  31.     DWORD   dwProcess = 1;
  32.     Q_ELEMENT qe;
  33.     LPTSTR  lptstr;
  34.     HANDLE  hProcess = GetCurrentProcess();
  35.     HANDLE  hClient;
  36.     int     iType = QUE_FIFO;
  37.     BOOL    bWait = FALSE;
  38.     int     iFlag;
  39.     BOOL    bPeek = FALSE;
  40.     int     iElement = -1;
  41.  
  42.  
  43.     while (iFlag = my_getopt( argc, argv, "plfwP"))
  44.         {
  45.         switch(iFlag)
  46.             {
  47.             case 'P':
  48.                 bPeek = TRUE;
  49.                 bWait = TRUE;
  50.                 break;
  51.  
  52.             case 'p':
  53.                 iType = QUE_PRIORITY;
  54.                 break;
  55.             case 'l':
  56.                 iType = QUE_LIFO;
  57.                 break;
  58.             case 'f':
  59.                 iType = QUE_FIFO;
  60.                 break;
  61.             case 'w':
  62.                 bWait = TRUE;
  63.                 break;
  64.             default:
  65.                 printf( "Unidentified flag %c\n", (char) iFlag);
  66.                 break;
  67.             }
  68.         }
  69.  
  70.  
  71.     if (bWait)
  72.         hClient = CreateEvent( NULL, TRUE, FALSE, "ClientSaysGo");
  73.  
  74.     dwErr = CreateQueue( &hq, iType, QUEUE_NAME);
  75.  
  76.     if (dwErr != 0)
  77.         {
  78.         printf( "CreateQueue Failed\n");
  79.         return(-1);
  80.         }
  81.  
  82.     hEvent = GetQueueEventHandle(hq);
  83.  
  84.     if (hEvent == 0)
  85.         {
  86.         CloseQueue(hq);
  87.         printf( "GetQueueEventHandle Failed\n");
  88.         return(-1);
  89.         }
  90.     else
  91.         printf( "GetQueueEventHandle Succeeded\n");
  92.  
  93.  
  94.     if (bWait)
  95.         WaitForSingleObject(hClient, INFINITE);
  96.  
  97.     while (bPeek)
  98.         {
  99.         SetPriorityClass(hProcess, HIGH_PRIORITY_CLASS);
  100.         WaitForSingleObject(hEvent, INFINITE);
  101.  
  102.         if (PeekQueue(hq, &qe, &iElement, FALSE) != 0)
  103.             {
  104.             bPeek = FALSE;
  105.             printf( "End of Peek's\n");
  106.             continue;
  107.             }
  108.  
  109.         SetPriorityClass(hProcess, NORMAL_PRIORITY_CLASS);
  110.  
  111.         printf( "E %5d,  P %5d, W %5d, S %5d, ",
  112.             qe.dwEventCode, qe.dwPriority, qe.dwWriterId, qe.dwShrHandle);
  113.  
  114.         if (qe.dwShrHandle != 0)
  115.             {
  116.             lptstr = (LPTSTR) ShrLock(qe.dwShrHandle);
  117.             printf( "M:%30s.\n", lptstr);
  118.             ShrUnlock(qe.dwShrHandle);
  119.             }
  120.  
  121.         }
  122.  
  123.     while (dwProcess)
  124.         {
  125.         SetPriorityClass(hProcess, HIGH_PRIORITY_CLASS);
  126.         WaitForSingleObject(hEvent, INFINITE);
  127.  
  128.         dwErr = ReadQueue(hq, &qe, 0, FALSE);
  129.  
  130.         SetPriorityClass(hProcess, NORMAL_PRIORITY_CLASS);
  131.  
  132.         if (dwErr != 0)
  133.             {
  134.             printf( "ReadQueue failed\n");
  135.             }
  136.  
  137.  
  138.         dwProcess = qe.dwEventCode;
  139.  
  140.         printf( "E %5d,  P %5d, W %5d, S %5d, ",
  141.             qe.dwEventCode, qe.dwPriority, qe.dwWriterId, qe.dwShrHandle);
  142.  
  143.         if (qe.dwShrHandle != 0)
  144.             {
  145.             lptstr = (LPTSTR) ShrLock(qe.dwShrHandle);
  146.             printf( "M:%30s.\n", lptstr);
  147.             ShrUnlock(qe.dwShrHandle);
  148.             ShrFree(qe.dwShrHandle);
  149.             }
  150.         }
  151.     CloseQueue(hq);
  152.     printf( "Server exiting\n");
  153.  
  154.     return(0);
  155. }
  156.